<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model{
	function __construct() {
		parent::__construct();
	}
	
	function check_login($user,$pwd){
		$clause = array(
			'user'=>$user,
			'password'=>md5($pwd)
		);
		$query_login = $this->db->get_where('user',$clause);
//                die($this->db->last_query());
		if($query_login->num_rows()>0){
			return $query_login->row_array();
		}else{
			return FALSE;
		}
	}
	
	function new_user($data){
		//cek apakah nim tsb sudah ada
		
			$data['password']=md5($data['password']);
                        
			$this->db->insert('user',$data);
			return $this->db->insert_id();
	}
	
	function update_user($data){
		$nim = $data['id'];
		unset ($data['id']);
                if($data['password']=='')
                    unset ($data['password']);
		$this->db->where('id',$id);
		$this->db->update('user',$data);
	}
	
	function get_user($id){
                
		$query = $this->db->get_where('user',array('id'=>$id));
		if($query->num_rows>0){
			return $query->row_array();
		}else{
			return FALSE;
		}
	}
	
	function get_all_user(){
		$query = $this->db->get('user');
		return $query->result_array();
	}
        
        function get_all_user_join_role(){
                $this->db->select('*, role.nama AS nama_role, user.nama AS nama_user, user.id AS id_user');
                $this->db->join('role','role_id=role.id','left');
		$query = $this->db->get('user');
//                die($this->db->last_query());
		return $query->result_array();
	}
	
	function delete_user($id){
		$this->db->delete('user',array('id'=>$id));
	}
	
	function is_access($id,$action='read')
	{
	    die($_SERVER["REQUEST_URI"]);
	}
}

/** End of file */